package com.swpu.service_archive.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_archive.entity.CheckupRecord;
import com.swpu.service_archive.entity.VaccineRecord;
import com.swpu.service_archive.service.CheckupRecordService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/service_archive/checkup-record")
public class CheckupRecordController {
    @Autowired
    private CheckupRecordService checkupRecordService;
    @PostMapping("/saveUserCheckupData")
    @Decrypt
    @Encrypt
    public R saveUserVaccineData(@RequestBody ResultEncryption resultEncryption, CheckupRecord checkupRecord, HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        checkupRecordService.saveUserCheckupData(openid,checkupRecord);
        return R.ok();
    }
    @PostMapping("/updateUserCheckupData")
    @Decrypt
    @Encrypt
    public R updateUserVaccineData(@RequestBody ResultEncryption resultEncryption, CheckupRecord checkupRecord,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        checkupRecordService.updateUserCheckupData(checkupRecord);
        return R.ok();
    }
    @GetMapping("getCheckupDataById/{id}")
    public R getVaccineDataById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        CheckupRecord checkupRecord = checkupRecordService.getCheckupDataById(id);
        return R.ok().data("checkupRecord",checkupRecord);
    }
    @GetMapping("getUserCheckupList")
    public R getUserVaccineList(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        List<CheckupRecord> checkupList = checkupRecordService.getUserCheckupList(openid);
        return R.ok().data("checkupList",checkupList);
    }
    @GetMapping("getCheckupRecordCount")
    public R getVaccineRecordCount(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        int checkupRecordCount = checkupRecordService.getCheckupRecordCount(openid);
        return R.ok().data("checkupRecordCount",checkupRecordCount);
    }
    @DeleteMapping("deleteCheckupById/{id}")
    public R deleteVaccineById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        checkupRecordService.deleteCheckupById(id);
        return R.ok();
    }
}

